package demo;
import java.io.*;
import java.util.*;
import java.util.regex.*;
/**
 * @Desciption:
 * @Author: feixiang.li
 * @date: 2024-07-29 14:38
 **/
public class Test3 {

    public static void main(String[] args) {
        String input = "D:\\code\\java-test\\demo1\\src\\main\\resources\\1.sql";
        File file = new File(input);
        StringBuilder sbf = new StringBuilder();
        BufferedReader reader = null;
        StringBuilder table_name_buff = new StringBuilder();
        int tablenum = 0;
        String tableString = "";
        FileWriter fileWriter = null;

        Map<String, String> sql_section_map = new HashMap<>();
        Map<String, String> table_section_map = new HashMap<>();
        Map<String, String> table_name_map = new HashMap<>();
        Map<String, String> table_description_map = new HashMap<>();
        Map<String, String> row_name_map = new HashMap<>();
        Map<String, String> map_row_type = new HashMap<>();
        Map<String, String> map_row_description = new HashMap<>();

        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;

            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr + "\n");
                if (tempStr.isEmpty()) {
                    sql_section_map.put("duan" + sql_section_map.size(), sbf.toString());
                    sbf.setLength(0);
                }
            }
            reader.close();

            String table_name_regex = "CREATE\\s+TABLE\\s+`(?<tableName>\\S*)`";   //匹配表名
            String table_description_regex = "COMMENT\\s*'(?<tableComment>[\u4e00-\u9fa5_a-zA-Z0-9\\S]*)'";   //匹配表描述
            String regex_row_description = "COMMENT\\s*'(?<columnComment>[\u4e00-\u9fa5_a-zA-Z0-9\\S]*)',";	//匹配列描述
            String table_row_name_regex = "  `(?<columnName>\\S*)`";   //匹配字段
            String regex_type = "  `(?<columnName>\\S*)`\\s+(?<columnType>\\S*)";   //匹配字段类型

            // 创建 Pattern 对象
            Pattern table_name_Pattern = Pattern.compile(table_name_regex, Pattern.CASE_INSENSITIVE);
            Pattern table_description_Pattern = Pattern.compile(table_description_regex, Pattern.CASE_INSENSITIVE);
            Pattern table_row_name_Pattern = Pattern.compile(table_row_name_regex, Pattern.CASE_INSENSITIVE);
            Pattern pattern_row_type = Pattern.compile(regex_type, Pattern.CASE_INSENSITIVE);
            Pattern pattern_row_description = Pattern.compile(regex_row_description, Pattern.CASE_INSENSITIVE);

            // 处理 SQL 分段
            for (Map.Entry<String, String> entry : sql_section_map.entrySet()) {
                tableString = entry.getValue();
                Matcher m1 = table_name_Pattern.matcher(tableString);

                if (m1.find()) {
                    table_name_buff.append(m1.group("tableName") + ",");
                    table_section_map.put("table" + tablenum, tableString);
                    tablenum++;
                }
            }

            // 解析表名
            String[] table_name_array = table_name_buff.toString().split(",");
            for (int i = 0; i < table_name_array.length; i++) {
                table_name_map.put("table" + i, table_name_array[i]);
            }

            // 解析表描述、列名、类型和描述
            for (Map.Entry<String, String> entry : table_section_map.entrySet()) {
                String section = entry.getValue();
                Matcher m2 = table_description_Pattern.matcher(section);
                if (m2.find()) {
                    table_description_map.put(entry.getKey(), m2.group("tableComment"));
                }

                String[] lines = section.split("\n");
                StringBuilder columns = new StringBuilder();
                StringBuilder types = new StringBuilder();
                StringBuilder descriptions = new StringBuilder();
                for (String line : lines) {
                    Matcher m3 = table_row_name_Pattern.matcher(line);
                    Matcher m4 = pattern_row_type.matcher(line);
                    Matcher m5 = pattern_row_description.matcher(line);

                    if (m3.find() && m4.find()) {
                        columns.append(m3.group("columnName")).append(",");
                        types.append(m4.group("columnType")).append(",");
                        if (m5.find()) {
                            descriptions.append(m5.group("columnComment")).append(",");
                        } else {
                            descriptions.append("null,");
                        }
                    }
                }

                row_name_map.put(entry.getKey(), columns.toString());
                map_row_type.put(entry.getKey(), types.toString());
                map_row_description.put(entry.getKey(), descriptions.toString());
            }

            // 组织输出 Markdown 文件内容
            StringBuilder text = new StringBuilder("# 数据库文档\n\n");
            text.append("## 数据表列表\n\n");

            for (Map.Entry<String, String> entry : table_section_map.entrySet()) {
                String tableName = table_name_map.get(entry.getKey());
                String tableDescription = table_description_map.get(entry.getKey());
                text.append("## 表名：").append(tableName);
                if (tableDescription != null && !tableDescription.isEmpty()) {
                    text.append(" (").append(tableDescription).append(")\n\n");
                } else {
                    text.append("\n\n");
                }

                text.append("字段名|数据类型|字段描述\n");
                text.append(":---:|:---:|:---:\n");

                String[] columnNames = row_name_map.get(entry.getKey()).split(",");
                String[] columnTypes = map_row_type.get(entry.getKey()).split(",");
                String[] columnDescriptions = map_row_description.get(entry.getKey()).split(",");
                for (int j = 0; j < columnNames.length; j++) {
                    String description = columnDescriptions[j];
                    if ("null".equals(description)) {
                        description = "";
                    }
                    text.append(columnNames[j]).append("|").append(columnTypes[j]).append("|").append(description).append("\n");
                }
            }

            // 文件输出
            fileWriter = new FileWriter("D:\\code\\java-test\\demo1\\src\\main\\resources\\1.md", false);
            fileWriter.write(text.toString());
            fileWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
